using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.IO;

namespace TXLooker.Common
{
    public static class CommonGlobal
    {
        // Methods
        public static void RaiseEventHandler(Delegate handler, object[] parameters)
        {
            if (!object.ReferenceEquals(handler, null))
            {
                foreach (Delegate delegate2 in handler.GetInvocationList())
                {
                    try
                    {
                        delegate2.DynamicInvoke(parameters);
                    }
                    catch (Exception exception)
                    {
                        LogManager.WriteMessageToCustomLog("Error in RaiseEventHandler " + exception.ToString());
                        Debug.WriteLine(exception.ToString());
                    }
                }
            }
        }

        public static void RaiseEventHandler(Delegate handler, object[] parameters, object syncObject)
        {
            try
            {
                if (object.ReferenceEquals(syncObject, null))
                {
                    throw new ArgumentException();
                }
                lock (syncObject)
                {
                    RaiseEventHandler(handler, parameters);
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine("Error in RaiseEventHandler " + exception.ToString());
                LogManager.WriteMessageToCustomLog("Error in RaiseEventHandler " + exception.ToString());
            }
        }

        public static void ReverseVariables(ref int var1, ref int var2)
        {
            var1 += var2;
            var2 = var1 - var2;
            var1 -= var2;
        }

        public static void SyncHandlerRemove(Delegate target, Delegate add, object syncObject)
        {
            if (object.ReferenceEquals(syncObject, null))
            {
                throw new ArgumentException();
            }
            lock (syncObject)
            {
                target = Delegate.Remove(target, add);
            }
        }

        public static void SyncHandlersCombine(Delegate target, Delegate add, object syncObject)
        {
            if (object.ReferenceEquals(syncObject, null))
            {
                throw new ArgumentException();
            }
            lock (syncObject)
            {
                target = Delegate.Combine(target, add);
            }
        }
    }

    public class XmlSerializer<T> where T : class
    {
        private static XmlSerializer Serializer
        {
            get
            {
                if (m_serializer == null)
                {
                    lock (typeof(XmlSerializer<T>))
                    {
                        if (m_serializer == null)
                            m_serializer = new XmlSerializer(typeof(T));
                    }
                }
                return m_serializer;
            }
        }
        private static XmlSerializer m_serializer = null;

        public static void Serialize(T item, string filepath)
        {
            using (FileStream fstream = new FileStream(filepath, FileMode.Create))
            {
                Serializer.Serialize(fstream, item);
            }
        }
        public static T Deserialize(string filepath)
        {
            object item = null;

            using (FileStream fstream = new FileStream(filepath, FileMode.Open))
            {
                item = Serializer.Deserialize(fstream);
            }

            return (T)item;
        }

    }
}
